//
//  BKMarketController.m
//  FastBee
//
//  Created by mac on 16/9/9.
//  Copyright © 2016年 xbk. All rights reserved.
//

#import "BKMarketController.h"

#import "BKLeftTableView.h"
#import "BKRightTableView.h"
#import "masonry.h"
#import "BKLeftTableViewModel.h"
#import "BKRightTableViewModel.h"
#import "BKLeftTableViewCell.h"
#import "BKRightTableViewCell.h"

@interface BKMarketController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) BKLeftTableView *leftView;
@property (nonatomic,strong) BKRightTableView *rightView;

@property (nonatomic,strong) NSArray *leftDataArr;
@property (nonatomic,strong) NSDictionary *rightDataDic;

@end

static NSString *leftID = @"leftID";
static NSString *rightID = @"rightID";

@implementation BKMarketController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    
    [self downloadLeftModel];

}

- (void)setupUI{

    //添加左边的tableView
    self.leftView = [[BKLeftTableView alloc]init];
    [self.view addSubview:self.leftView];
    
    [self.leftView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.top.bottom.equalTo(self.view);
        make.width.equalTo(@100);
        
    }];
    
    //添加右边的tableView
    self.rightView = [[BKRightTableView alloc]init];
    [self.view addSubview:self.rightView];
    
    [self.rightView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(self.leftView.mas_right);
        make.right.bottom.equalTo(self.view);
        make.top.equalTo(self.view.mas_top).offset(64);
        
    }];
    
    self.leftView.showsVerticalScrollIndicator = NO;
    
    self.leftView.dataSource = self;
    self.leftView.delegate = self;
    self.rightView.dataSource = self;
    self.rightView.delegate = self;
    
    self.leftView.rowHeight = 50;
    self.rightView.rowHeight = 70;
    
    
    [self.leftView registerClass:[BKLeftTableViewCell class] forCellReuseIdentifier:leftID];
    [self.rightView registerClass:[BKRightTableViewCell class] forCellReuseIdentifier:rightID];
}

//视图加载出来后默认选中第一组
- (void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];
    
    [self.leftView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
}


//下载网路数据
- (void)downloadLeftModel{

    [BKLeftTableViewModel downloadDataWithUrl:@"supermarket.json.php" successBlock:^(NSArray *arr,NSDictionary *dic) {
        
        //NSLog(@"%@",arr);
        self.leftDataArr = arr;
        self.rightDataDic = dic;
        
        [self.leftView reloadData];
        [self.rightView reloadData];
        
    } failBlock:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
    
    
    
}


/**
 *  responseObject = @{@"data":@{ @"product":@{@"a152":@[],@"a136":@[]} @"category":@[]}}
 
 @{ @"product":@{@"a152":@[],@"a136":@[]}}
 @{ @"category":@[]}
 
 
 NSDictionary *dic = [responseObject objectForKey:@"data"];
 NSArray *arr = dic[@"categories"];
 
 NSDictionary *rightDic = dic[@"products"];
 
 NSMutableArray *arrM = [NSMutableArray arrayWithCapacity:arr.count];
 
 for (NSDictionary *dic in arr) {
 
 BKLeftTableViewModel *model = [BKLeftTableViewModel leftModelWithDic:dic];
 
 [arrM addObject:model];
 }
 */

- (NSArray *)rightdataWithUid:(NSString *)myID{

    //根据product获取了一个字典,也就是这个rightDataDic @{@"a152":@[],@"a136":@[]}
    //然后需要字典转模型.所以就根据key,取出值,也就是数组.
    //这个key,是从左边视图的model中获取得.
    NSArray * arr = self.rightDataDic[myID];
    
    NSMutableArray *arrM = [NSMutableArray arrayWithCapacity:arr.count];
    
    for (NSDictionary *dic in arr) {
        
        BKRightTableViewModel *model = [BKRightTableViewModel rightModelWithDic:dic];
        [arrM addObject:model];
    }
    return arrM.copy;
}


#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    //左边的行数决定了右边的组数
    if (tableView == self.leftView) {
        return 1;
    }else{
    
        return self.leftDataArr.count;
    }
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    if (tableView == self.leftView) {
        return self.leftDataArr.count;
    }else{
    
        //  rightDataDic @{@"a152":@[],@"a136":@[]}
        BKLeftTableViewModel *model = self.leftDataArr[section];
        NSArray *arr = self.rightDataDic[model.myID];
        return arr.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (tableView == self.leftView) {
        
        BKLeftTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:leftID forIndexPath:indexPath];
       
        cell.model = self.leftDataArr[indexPath.row];
        cell.backgroundColor = [UIColor colorWithRed:222/255.0 green:222/255.0 blue:222/255.0 alpha:1.0];
        
        UIView *selectedView = [[UIView alloc]initWithFrame:cell.frame];
        selectedView.backgroundColor = [UIColor whiteColor];
        cell.selectedBackgroundView = selectedView;
        
        UIView *yellowView = [[UIView alloc]initWithFrame:CGRectMake(0, 5, 5, 30)];
        [selectedView addSubview:yellowView];
        yellowView.backgroundColor = [UIColor yellowColor];
        
        return cell;
        
    }else{
    
        BKRightTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:rightID forIndexPath:indexPath];
        
        //右边的第一组对应左边的第一行
        BKLeftTableViewModel *model = self.leftDataArr[indexPath.section];
        NSArray *arr = [self rightdataWithUid:model.myID];
        cell.model = arr[indexPath.row];
        //NSLog(@"%@",cell.model);
        cell.textLabel.text = cell.model.name;
        
        return cell;
    }
}

#pragma mark - UITableViewDelegate
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

    if (tableView == self.rightView) {
        
        BKLeftTableViewModel *model = self.leftDataArr[section];
        return model.name;
    }
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    if (tableView == self.leftView) {
        
        [self.rightView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:indexPath.row] atScrollPosition:UITableViewScrollPositionTop animated:NO];
    }else{
    
        return;
    }
    
}

//组头图视图将要显示的时候调用
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section{

    if (tableView == self.rightView) {
        
        //获取当前cell属于第几行
        NSArray *arr = [tableView indexPathsForVisibleRows];
        //NSLog(@"%@",indexPath);
        NSIndexPath *indexPath = arr.firstObject;
        
        [self.leftView selectRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.section inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];
    }
}

//当组头视图结束显示后调用
- (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section{

    if (tableView == self.rightView) {
        
        //获取当前cell属于第几行
        NSArray *arr = [tableView indexPathsForVisibleRows];
        //NSLog(@"%@",indexPath);
        NSIndexPath *indexPath = arr.firstObject;
        
        [self.leftView selectRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.section inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];
    }
}

//- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
//
//    if (scrollView == self.rightView) {
//        
//        //获取当前cell属于第几行
//        NSArray *arr = [self.rightView indexPathsForVisibleRows];
//        //NSLog(@"%@",indexPath);
//        NSIndexPath *indexPath = arr.firstObject;
//        
//        [self.leftView selectRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.section inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];
//    }
//}


@end
